﻿# -*- coding:utf-8 -*-
import datetime
from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.template import RequestContext, Context
from django.core.urlresolvers import reverse

from ktit.storage.forms import CategoryForm, PartForm, DocumentForm,  DocumentReceiptForm, DocumentDeliveryForm 
#from ktit.storage.models import Category, Document, PartDocument, PartItem, DocumentReceipt
#from ktit.storage.models import Part
from ktit.storage.models import Category, Part, PartItem, Document, DocumentItem, DocumentReceipt, DocumentDelivery, Unit, Place
from ktit.storage.categories import create_cat_tree
#from ktit.storage.historypath import *

from ktit.storage.contextprocessors import category_proc




@login_required
#def viewcategory(request, catid):
def view(request, catid):
    category = get_object_or_404(Category, id=catid)
    request.session["current_category"] = int(catid)    
    partlist = Part.objects.select_related().filter(category=catid)
    context = Context({
      'category':category,
      'partlist': partlist,
      'current_menu_item' : '2',
      'current_sub_menu_item':'1',      
    })    
    return render_to_response('part_list.html', context, context_instance=RequestContext(request, processors=[category_proc]))



@login_required
#def category_colaps(request, catid):
#def colapscategory(request, catid):
def collaps(request, catid):
    if request.session["current_category"] == None:
        request.session["current_category"] = catid
    if int(catid) in request.session["expanded_catid"]:
        request.session["expanded_catid"].remove(int(catid))
    partlist = Part.objects.select_related().filter(category=request.session["current_category"])
    context = Context({
      'partlist' : partlist,
      'current_menu_item' : '2',
      'current_sub_menu_item':'1',  
    })
    return render_to_response('part_list.html', context,  context_instance=RequestContext(request, processors=[category_proc]))



@login_required
#def category_expand(request, catid):
def expand(request, catid):
    if request.session["current_category"] == None:
        request.session["current_category"] = catid
    if id not in request.session["expanded_catid"]:
        request.session["expanded_catid"].append(int(catid))    
    partlist = Part.objects.select_related().filter(category=request.session["current_category"])
    context = Context({
      'partlist' : partlist,
      'current_menu_item' : '2',
      'current_sub_menu_item':'1',        
    })        
    return render_to_response('part_list.html', context, context_instance=RequestContext(request, processors=[category_proc]))



@login_required
#def category_edit(request, catid):
#def editcategory(request, catid):
def edit(request, catid):
    #history_path_add(request, HP_EDIT_CATEGORY)
    cat_inst = Category.objects.get(id=catid)
    if request.method == 'POST':
        form = CategoryForm(request.POST, instance=cat_inst)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect( reverse('ktit.storage.views.categorys.view', args=[(request.session["current_category"])]) )
    else:
        form = CategoryForm(instance=cat_inst)
    return render_to_response('form_category_edit.html', locals(), context_instance=RequestContext(request, processors=[category_proc]))





@login_required
#def category_new(request):
#def addcategory(request):
def create(request):
    #history_path_add(request, HP_NEW_CATEGORY)
    if request.method == 'POST':
        form = CategoryForm(request.POST)
        if form.is_valid():
            cat = form.save()
            request.session["current_category"] = cat.id
            return HttpResponseRedirect(reverse('ktit.storage.views.categorys.view', args=[cat.id]))
    else:
        form = CategoryForm(initial={'parent': request.session["current_category"]})
    return render_to_response('form_category_add.html', locals(), context_instance=RequestContext(request, processors=[category_proc]))










@login_required
#def category_delete(request, item):
def delete(request, item):
    pass
    
   
